package com.icim.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.swing.BoxLayout;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.icim.entity.Ret_cus_prd_in;
import com.icim.entity.Ret_cus_so;
import com.icim.entity.Ret_prd_info;

@Repository
public class RetCusPrdInDao {
    @Resource
    private JdbcTemplate icimJdbcTemplate;

    public void save(Ret_cus_prd_in retCusPrdIn) {
        StringBuffer sqlBuffer = new StringBuffer("INSERT INTO RET_CUS_PRD_IN(");
        sqlBuffer.append("LOG_ID,RCV_TIMESTAMP,OPE_FAB_ID,BOX_PHY_ID,MTRL_BOX_ID,");
        sqlBuffer.append("CUS_GROUP_ID,MTRL_PALLET_NO,MTRL_PALLET_ID,MTRL_PROD_ID_FK,BLOCK_SIZE,");
        sqlBuffer.append("PART_NO,X_AXIS_CNT_FK,Y_AXIS_CNT_FK,PRD_TYP,PRD_CATE,");
        sqlBuffer.append("SLIP_NO,BOX_CAPACITY,BLOCK_QTY,ACT_FLG,MTRL_PALLET_CAPACITY");
        sqlBuffer.append(") VALUES(");
        sqlBuffer.append("?,?,?,?,?,");
        sqlBuffer.append("?,?,?,?,?,");
        sqlBuffer.append("?,?,?,?,?,");
        sqlBuffer.append("?,?,?,?,?");
        sqlBuffer.append(")");

        icimJdbcTemplate.update(
                sqlBuffer.toString(),
                new Object[] { retCusPrdIn.getLogId(), retCusPrdIn.getRcvTimestamp(), retCusPrdIn.getOpeFabId(),
                        retCusPrdIn.getBoxPhyId(), retCusPrdIn.getMtrlBoxId(), retCusPrdIn.getCusGroupId(),
                        retCusPrdIn.getMtrlPalletNo(), retCusPrdIn.getMtrlPalletId(), retCusPrdIn.getMtrlProdIdFk(),
                        retCusPrdIn.getBlockSize(), retCusPrdIn.getPartNo(), retCusPrdIn.getxAxisCntFk(),
                        retCusPrdIn.getyAxisCntFk(), retCusPrdIn.getPrdTyp(), retCusPrdIn.getPrdCate(),
                        retCusPrdIn.getSlipNo(),retCusPrdIn.getBoxCapacity(),retCusPrdIn.getBlockQty(),retCusPrdIn.getActFlg(),retCusPrdIn.getMtrlPalletCapacity()});
    };
    
    public List<Ret_cus_prd_in> getMtrlBoxList(List<String> boxIdList) {
        StringBuffer sqlBuffer = new StringBuffer( "SELECT BOX_PHY_ID, MTRL_BOX_ID,MTRL_PALLET_CAPACITY FROM RET_CUS_PRD_IN WHERE MTRL_BOX_ID IN (");
        for (String boxId : boxIdList) {
            sqlBuffer.append("'").append(boxId).append("'");
            if(boxIdList.indexOf(boxId)< boxIdList.size()-1){
                sqlBuffer.append(",");
            }
        }
        sqlBuffer.append(")");
        return icimJdbcTemplate.query(sqlBuffer.toString(), new RowMapper<Ret_cus_prd_in>() {
            @Override
            public Ret_cus_prd_in mapRow(ResultSet rs, int rowNum) throws SQLException {
                Ret_cus_prd_in retCusPrdIn = new Ret_cus_prd_in();
                retCusPrdIn.setBoxPhyId(rs.getString("BOX_PHY_ID"));
                retCusPrdIn.setMtrlBoxId(rs.getString("MTRL_BOX_ID"));
                retCusPrdIn.setMtrlPalletCapacity(rs.getInt("MTRL_PALLET_CAPACITY"));
                return retCusPrdIn;
            }
        });

    }
}
